<?php
class Db_upload_image_product extends MVC_SQL {
	public function __construct(){
		$this->setTable('ams_pictures');
	}
	
	public function getListupload_image_product($oModel)
	{
		$oModel->oNavigator = new paging();
		$page_size = $oModel->getData('page_size');
		$pagegroup_size = $oModel->getData('pagegroup_size');
		$stylesheet = $oModel->getData('stylesheet');
		
		$sSql	= "SELECT * FROM ams_pictures ";
		$sSql	.= " ORDER BY ams_pictures.pictures_date_uploaded DESC";
		$total_record = $this->getNumRow($sSql);
		$oModel->oNavigator->init($sSql, $total_record, $page_size, $pagegroup_size, 'pUser', $stylesheet);
		$sSql = $oModel->oNavigator->getSql();
		return $this->getRecordsetExtend($sSql);
	}
	
	public function getListProvince($oModel)
	{
		$sql = "SELECT * FROM ams_province ORDER BY province_sort ASC";
		return  $this->getRecordsetExtend($sql);
	}	
	public function deleteImageOfupload_image_product($upload_image_product_id){
		$sql = "DELETE FROM ams_pictures WHERE pictures_upload_image_product_id ='{$upload_image_product_id}' ";
		return  $this->db_query($sql);
	}
		
	public function updateupload_image_productStatus($aData)
	{
		return $this->doUpdateByPK($aData);
	}
			
	public function getInfoupload_image_product($upload_image_product_id){
		return $this->getRecordsetByPK($upload_image_product_id);
	}			
	public function filterupload_image_product($oModel)
	{		
		$oModel->oNavigator = new paging();
		$page_size = $oModel->getData('page_size');
		$pagegroup_size = $oModel->getData('pagegroup_size');
		$stylesheet = $oModel->getData('stylesheet');
		
		$fromDate = formatDateToDb($oModel->getData('fromDate'));
		$toDate = formatDateToDb($oModel->getData('toDate'));
		
		$sSql	 = " SELECT p.* FROM ams_pictures AS p ";
		$sSql	.= " WHERE ( date_format(p.pictures_date_uploaded,'%Y-%m-%d') >= '{$fromDate}' ";
		$sSql	.= " AND date_format(p.pictures_date_uploaded,'%Y-%m-%d') <= '{$toDate}' ) ";
		$sSql	.= " ORDER BY p.pictures_date_uploaded DESC ";
		$total_record = $this->getNumRow($sSql);
		$oModel->oNavigator->init($sSql, $total_record, $page_size, $pagegroup_size, 'pUser', $stylesheet);
		$sSql = $oModel->oNavigator->getSql();
		return $this->getRecordsetExtend($sSql);
	}
	public function getImage($pictures_product_id){
		$sql = "SELECT product_pictures FROM ams_product WHERE product_id = '{$pictures_product_id}'";
		$getListIdImage =$this->getRecordsetExtend($sql);
			if (count($getListIdImage)==0) {
				return 'no found product id';
				} elseif (count($getListIdImage)>1){
					return 'once more than one product id';
				}
		foreach ($getListIdImage as $data){
			$array_id_iamge = explode(" ",$data['product_pictures']);
		}
		
		$sWhere	=	'';
		$xxx = count($array_id_iamge);
		$ZZZ =0;
		foreach ($array_id_iamge as $pictures_id){
			$ZZZ++;
			$sWhere .= " (pictures_id = '{$pictures_id}')";
			if ($ZZZ<$xxx) {
				$sWhere .= " OR ";
			}
		}
			
		
				
		if ($sWhere!='') {
			

				$sql = "SELECT * FROM ams_pictures WHERE  ".$sWhere." LIMIT 3 ";
				return $this->getRecordsetExtend($sql);			
				}
				
		}
	public function getAllImage_db($oModel){
		$sql = "SELECT * FROM ams_pictures GROUP BY pictures_file_name ORDER BY pictures_date_uploaded DESC ";
		return $this->getRecordsetExtend($sql);
		}
/*Do upadate upadate file name */
/*	public function doUpdateImagesItemProduct($aData){		
		$sql_check_first = "SELECT * FROM ams_pictures WHERE (pictures_product_id = '') AND (pictures_id = '{$aData['pictures_id']}')";
		$check_first = $this->getRecordsetExtend($sql_check_first);
		if (is_array($check_first)) 		
		{
			return $this->doUpdateByPK($aData);
			}
		else {
			$sql_data =  "SELECT * FROM ams_pictures WHERE (pictures_id = '{$aData['pictures_id']}') LIMIT 1";
			$data_I = $this->getRecordsetExtend($sql_data);
			if (is_array($data_I)) 
				foreach ($data_I as $data){
					$sql_insert_more = "INSERT INTO ams_pictures(pictures_product_id,pictures_file_name,pictures_comment,pictures_title,
																 pictures_note,pictures_date_uploaded) 
													 VALUES('{$aData['pictures_product_id']}',
															'{$data['pictures_file_name']}','{$data['pictures_comment']}',
															'{$data['pictures_title']}','{$data['pictures_note']}',
															'{$data['pictures_date_uploaded']}')";
				return $this->db_query($sql_insert_more);
			}
		}
	}*/
/*
//updated by: tienhv
//date: 24th April 2009
*/
public function doUpdateImagesItemProduct($aData){		

		$sql_data 	=  "SELECT * FROM ams_pictures WHERE (pictures_id = '{$aData['pictures_id']}') LIMIT 1";
		$sql_data_2 =  "SELECT  product_pictures,product_pictures_comment FROM ams_product WHERE (product_id = '{$aData['pictures_product_id']}') LIMIT 1";
		$data_I 	= $this->getRecordsetExtend($sql_data);
		$data_II 	= $this->getRecordsetExtend($sql_data_2);
		if (is_array($data_II)) {
			foreach ($data_II as $data_product)
				$array_data_II = array('product_pictures'=>$data_product['product_pictures'],
										'product_pictures_comment'=>$data_product['product_pictures_comment']);
				$check_exist_image = explode(" ",$array_data_II['product_pictures']);
			if (count($check_exist_image)>=3) 
				return 0;
			
			foreach ($check_exist_image as $pictures_id)
					if ($pictures_id==$aData['pictures_id']) 
						return 0;
						
			}
	 	else return 0;
	 	
	 	if (is_array($data_I))
	 		foreach ($data_I as $data){
	 			$product_pictures 				= trim($array_data_II['product_pictures']." ".$data['pictures_id']);
	 			$product_pictures_comment		= trim($array_data_II['product_pictures_comment']." 139211 ".$data['pictures_comment']);
				$final_data = array("product_pictures"=>$product_pictures,
									"product_pictures_comment"=>$product_pictures_comment);
			
			} else return 0;
		
// execute update
	$sql_update = "UPDATE ams_product SET product_pictures ='{$final_data['product_pictures']}',
										product_pictures_comment ='{$final_data['product_pictures_comment']}' 
					WHERE product_id = 	'{$aData['pictures_product_id']}'";
	return $this->db_query($sql_update);
		
	}
/*
//*********************
*/
public function deleteImageList($aData){
	
	$sql_data =  " SELECT  product_pictures,product_pictures_comment FROM ams_product WHERE (product_id = '{$aData['pictures_product_id']}') LIMIT 1";
	$check_first = $this->getRecordsetExtend($sql_data);
	$pos= '139211LLLLL';
	if (is_array($check_first)) {
		foreach ($check_first as $cacheData)
			{
				$cache_product_pictures 		= explode(" ",$cacheData['product_pictures']);
				$cache_product_pictures_comment = explode("139211",$cacheData['product_pictures_comment']);
			}
			$i=	-1;
		foreach ($cache_product_pictures as $image_id){
			$i++;
				if ($image_id==$aData['pictures_id']) {
					$pos=$i;
					break;
				}
			}
		}

	$new_product_pictures='';
	$new_product_pictures_comment='';
	if ($pos != '139211LLLLL') {
		for ($i=0;$i<count($cache_product_pictures); $i++)
				if ($i!=$pos) 
					{
						$new_product_pictures.=$cache_product_pictures[$i]." ";
						$new_product_pictures_comment.=$cache_product_pictures_comment[$i]." ";
					}
		} else	return 0;
		
		$new_product_pictures= trim($new_product_pictures);
		$new_product_pictures_comment= trim($new_product_pictures_comment);
		$sql_update = "UPDATE ams_product SET product_pictures ='{$new_product_pictures}',
										product_pictures_comment ='{$new_product_pictures_comment}' 
					WHERE product_id = 	'{$aData['pictures_product_id']}'";
			return $this->db_query($sql_update);		
		
}
	public function filter_image($aData){
		$where =' 1 ';
		if (intval($aData['and_or'])==1) {
			if (trim($aData['pictures_file_name'])!='') 
				$where .= " AND (pictures_file_name LIKE '%{$aData['pictures_file_name']}%' ) ";
			if (trim($aData['pictures_title'])!='') 
				$where .= " AND (pictures_title LIKE '%{$aData['pictures_title']}%' ) ";
			if (trim($aData['pictures_note'])!='')
				$where .= " AND (pictures_note LIKE '%{$aData['pictures_note']}%' ) ";
		} else {
			if (trim($aData['pictures_file_name'])!='') 
				$where .= " OR (pictures_file_name LIKE '%{$aData['pictures_file_name']}%' ) ";
			if (trim($aData['pictures_title'])!='') 
				$where .= " OR (pictures_title LIKE '%{$aData['pictures_title']}%' ) ";
			if (trim($aData['pictures_note'])!='')
				$where .= " OR (pictures_note LIKE '%{$aData['pictures_note']}%' ) ";
			
		}
		$sql = "SELECT * FROM ams_pictures WHERE ";
		$sql.= $where;
		$sql.= " GROUP BY pictures_file_name ";
		$sql.= " ORDER BY pictures_date_uploaded DESC LIMIT 50 ";
		
		return $this->getRecordsetExtend($sql);
		}
		
		public function getTotalPicInDay()
		{
			$today=date('Y-m-d');
			$sSql="SELECT * FROM ams_pictures WHERE pictures_date_uploaded = '{$today}' ";
			return $this->getNumRow($sSql);
		}
		
		public function GenPicsId()
		{
			$num=$this->getTotalPicInDay();
			$num++;
			$temp_num="";
			if(5-strlen($num)>0)
			{
				
				for($i=0;$i<5-strlen($num);$i++)
				{
					$temp_num.="0";
				}	
			}
			$num=$temp_num.$num;
			return date('Ymd').$num;
		}
		
	
/*
*
*
*/
/*END CLASS*/
}
?>